package com.lam.common.components.dict.model;

import com.lam.common.components.dict.annotation.Dict;
import com.lam.common.components.dict.constant.DictTypeEnum;
import com.lam.common.utils.StringUtils;

public class DictDesc {
	
	/** 要转换的字段名称 */
	private String fieldName;
	/** 转换字典后的值赋值到的字段名 */
	private String targetField;
	/** 转换字典后的值是否赋值到原字段 */
	private boolean targetUseOrig;
	
	/** 是否反向转换字典，false则表示正向转换(value=>label)，true则表示反向转换(value=>label) */
	private boolean reverse;
	
	/** 是否删除原字段 */
	private boolean removeOrig;
	
	private String dictCode;
	
	private DictTypeEnum dictType;
	
	private TableDictInfo tableDictInfo;
	
	public DictDesc(String fieldName, Dict dict) {
		this.fieldName = fieldName;
		this.dictCode = dict.dictCode();
		this.dictType = dict.dictType();
		
		if(StringUtils.isNoneBlank(dict.tableName(), dict.valueField(), dict.textField())) {
			this.tableDictInfo = new TableDictInfo(dict.tableName(), dict.valueField(), dict.textField(), dict.conditionSql());
		}
		this.targetField = dict.targetField();
		this.reverse = dict.reverse();
		this.targetUseOrig = dict.targetUseOrig();
		if(this.targetUseOrig) {
			this.targetField = fieldName;
		}
		if(StringUtils.isNotBlank(dict.origField())) {
			this.fieldName = dict.origField();
		}
	}
	
	public DictDesc(String fieldName, String dictCode) {
		this(fieldName, dictCode, DictTypeEnum.SIMPLE_DICT);
	}
	
	public DictDesc(String fieldName, String dictCode, DictTypeEnum dictType) {
		this.fieldName = fieldName;
		this.dictCode = dictCode;
		this.dictType = dictType;
	}
	
	public DictDesc(String fieldName, TableDictInfo tableDictInfo) {
		this.fieldName = fieldName;
		this.tableDictInfo = tableDictInfo;
	}
	
	public boolean isTableDictWithCondition() {
		return tableDictInfo != null && tableDictInfo.isWithCondition();
	}
	
	public String getFieldName() {
		return fieldName;
	}
	public String getDictCode() {
		return dictCode;
	}

	public boolean isRemoveOrig() {
		return removeOrig;
	}

	public DictDesc removeOrig(boolean removeOrig) {
		this.removeOrig = removeOrig;
		return this;
	}

	public String getTargetField() {
		return targetField;
	}

	public DictDesc targetField(String targetField) {
		this.targetField = targetField;
		return this;
	}

	public boolean isTargetUseOrig() {
		return targetUseOrig;
	}

	public DictDesc targetUseOrig(boolean targetUseOrig) {
		this.targetUseOrig = targetUseOrig;
		if(this.targetUseOrig) {
			this.targetField = this.fieldName;
		}
		return this;
	}

	public boolean isReverse() {
		return reverse;
	}

	public DictDesc reverse(boolean reverse) {
		this.reverse = reverse;
		return this;
	}

	public TableDictInfo getTableDictInfo() {
		return tableDictInfo;
	}

	public void setTableDictInfo(TableDictInfo tableDictInfo) {
		this.tableDictInfo = tableDictInfo;
	}

	public DictTypeEnum getDictType() {
		return dictType;
	}

}
